package com.xnewtech.xplatform.swan.register.modular.modname.service.impl;

import com.xnewtech.xplatform.swan.libs.mp.page.PageFactory;
import com.xnewtech.xplatform.swan.model.page.PageResult;
import com.xnewtech.xplatform.swan.register.modular.modname.entity.TestDemo;
import com.xnewtech.xplatform.swan.register.modular.modname.mapper.TestDemoMapper;
import com.xnewtech.xplatform.swan.register.modular.modname.model.params.TestDemoParam;
import com.xnewtech.xplatform.swan.register.modular.modname.model.result.TestDemoResult;
import  com.xnewtech.xplatform.swan.register.modular.modname.service.TestDemoService;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * demo表 服务实现类
 * </p>
 *
 * @author xnew
 * @since 2020-11-26
 */
@Service
public class TestDemoServiceImpl extends ServiceImpl<TestDemoMapper, TestDemo> implements TestDemoService {

    @Override
    public void add(TestDemoParam param){
        TestDemo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(TestDemoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(TestDemoParam param){
        TestDemo oldEntity = getOldEntity(param);
        TestDemo newEntity = getEntity(param);
        BeanUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public TestDemoResult findBySpec(TestDemoParam param){
        return null;
    }

    @Override
    public List<TestDemoResult> findListBySpec(TestDemoParam param){
        return null;
    }

    @Override
    public PageResult<TestDemoResult> findPageBySpec(TestDemoParam param){
        Page pageContext = getPageContext();
        IPage<TestDemoResult> page = this.baseMapper.customPageList(pageContext, param);
        return new PageResult<>(page);
    }

    private Serializable getKey(TestDemoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return PageFactory.defaultPage();
    }

    private TestDemo getOldEntity(TestDemoParam param) {
        return this.getById(getKey(param));
    }

    private TestDemo getEntity(TestDemoParam param) {
        TestDemo entity = new TestDemo();
        BeanUtil.copyProperties(param, entity);
        return entity;
    }

}
